#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/9/9 18:41
# @Author : lenovo
# @Email : 26541796406@qq.com
# @File : CRM_ST_BG_098.py
# @Project : crm
"""添加日程正确输入信息 """
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
"""选择Chrome浏览器，将driverAlert化，打开crm网页"""
driver = webdriver.Chrome()
alert = Alert(driver)
driver.get('http://192.168.1.139/crm3')
sleep(2)
"""找到用户名，密码输入框以及登录按钮"""
username_locator = driver.find_element_by_name('name')
password_locator = driver.find_element_by_name('password')
login_locator = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/form/fieldset/input[3]")
"""输入用户名，密码，点击登录"""
username_locator.send_keys('admin')
password_locator.send_keys('123456')
login_locator.click()
sleep(3)
"""
 这是在获取cookies值
 以后就可以免登录，直接绕过登录页面
cookies = driver.get_cookies()
cookie = {}
for k,v in cookies[0].items():
    if k == 'name' or k == 'value':
        cookie[k] = v
print(cookie)
"""
cookie = {'name': 'PHPSESSID', 'value': 'i1mj8tbl7cp4pt11rijh3toul0'}
driver.get('http://192.168.1.139')
driver.add_cookie(cookie)
"""直接获得添加日程的网址"""
driver.get('http://192.168.1.139/crm3/index.php?m=event&a=add')
sleep(2)
"""进行添加日程的操作"""
# 创建定位器
schedule_theme_locator = (By.ID,'subject')  # 日程主题
start_date_locator = (By.ID,'start_date')  # 日程开始时间
adress_locator = (By.ID,'venue')  # 日程地点
end_date_locator = (By.ID,'end_date') # 日程结束时间
select1_locator = driver.find_element(By.ID,'select1')  # 选择相关下拉框
module_name_locator = (By.ID,'module_name')  # 联系人选项
contact_locator = (By.XPATH,'/html/body/div[8]/div[2]/table/tbody[2]/tr/td[1]/input')  # 选择吊炸天
schedule_ok_locator = (By.XPATH,'/html/body/div[8]/div[3]/div/button[1]/span')  # 确认按钮
description_locator = (By.XPATH,'/html/body/div[5]/div[2]/div/form/table/tbody/tr[6]/td[2]/textarea')  # 描述输入框
schedule_submit_locator = (By.CSS_SELECTOR,'input[value="保存"]')  # 保存按钮
# 具体操作
driver.find_element(*schedule_theme_locator).send_keys('季度会')
sleep(2)
driver.find_element(*start_date_locator).send_keys('2021-09-28')
sleep(2)
driver.find_element(*adress_locator).send_keys('会议室')
sleep(2)
driver.find_element(*end_date_locator).send_keys('2021-09-28')
sleep(2)
select = Select(select1_locator)
sleep(2)
select.select_by_value('contacts')
driver.find_element(*module_name_locator).click()
sleep(2)
driver.find_element(*contact_locator).click()
sleep(2)
driver.find_element(*schedule_ok_locator).click()
sleep(2)
driver.find_element(*description_locator).send_keys('谁要是迟到就直接让他滚蛋')
sleep(2)
driver.find_element(*schedule_submit_locator).click()

sleep(2)
driver.close()